package com.baomidou.springwind.controller.supply;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.*;
import com.baomidou.springwind.service.NetworkSupplyService;
import com.baomidou.springwind.mapper.NetworkSupplyDao;
import com.baomidou.springwind.mapper.NetworkDepotDao;
import com.baomidou.springwind.mapper.NetworkDepotGoodsDao;
import com.baomidou.springwind.mapper.NetworkReliabilityDao;
import com.baomidou.springwind.service.NetworkTransportPropertyService;
import com.baomidou.springwind.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物流网络规划运输属性表
 * </p>
 *
 * @Author huangtao
 * @Date 2017-8-21
 */
@Controller
@RequestMapping("/network/supply")
public class TransportPropertyController extends BaseController {

    @Autowired
    private NetworkSupplyService networkSupplyService;

    @Autowired
    private NetworkSupplyDao networkSupplyDao;

    @Autowired
    private ProjectService projectService;
    @Autowired
    private NetworkTransportPropertyService propertyService;
    @Autowired
    private NetworkReliabilityDao networkReliabilityDao;
    @Autowired
    private NetworkDepotDao networkDepotDao;
    @Autowired
    private NetworkDepotGoodsDao networkDepotGoodsDao;
    /**
     * 运输属性
     */
    @RequestMapping("/transport/property")
    @Permission("1001")
    public String transport(Model model){
        EntityWrapper<Project> wrapper = new EntityWrapper<Project>();
        wrapper.eq("operator_id",getCurrentUserId());
        wrapper.in("state", Arrays.asList("1", "2"));
        model.addAttribute("prjList", projectService.selectList(wrapper));
        return pjaxPath("/file/transport_property_list", model);
    }

    /**
     * 运输属性数据--ajax获取数据
     */
    @ResponseBody
    @RequestMapping("/transport/property/getData")
    @Permission("1001")
    public String getData(Page<NetworkTransportProperty> page) {
        page = new Page<NetworkTransportProperty>(page.getCurrent(), page.getSize());
        EntityWrapper<NetworkTransportProperty> wrapper = new EntityWrapper<NetworkTransportProperty>();
        String batchId=request.getParameter("batchId");
        if (batchId==null){
            batchId=getProjectId();
            if(batchId.length()==0){
                batchId="-1";
            }
        }
        wrapper.eq("batch_id",batchId);
        wrapper.eq("operator_id", getCurrentUserId());
        wrapper.orderBy("create_time", false);
        page = propertyService.selectPage(page, wrapper);
        List<NetworkTransportProperty> propertyList = page.getRecords();
        if (propertyList.size() > 0) {
            Project project = projectService.selectById(propertyList.get(0).getBatchId());
            for (NetworkTransportProperty property : propertyList) {
                property.setBatchName(project == null ? "--" : project.getName());
            }
        }
        return jsonPage(page);
    }



}
